<?php

namespace catchAdmin\archives\controller;

use app\common\business\UserCenter;
use app\common\model\Operation;
use catchAdmin\archives\service\InformationService;
use catcher\base\CatchController;
use catcher\base\CatchRequest;
use catcher\CatchResponse;
use app\common\model\PatientInfo as PatientInfoModel;
use app\common\model\PatientRecord as PatientRecordModel;
use app\common\model\Relation as RelationModel;
use app\common\model\Source as SourceModel;
use catchAdmin\archives\service\DetailedService;
use catcher\exceptions\CatchException;
use catcher\exceptions\FailedException;
use app\common\model\PatientDischargeSummary as SummaryModel;

class Detailed extends CatchController
{
    protected $patientInfo;

    public function __construct(PatientInfoModel $patientInfo)
    {
        $this->patientInfo = $patientInfo;
    }

    /**
     * 患者详情
     *
     * @time 2020年01月09日
     * @param string $id
     * @return \think\response\Json
     */
    public function patient_detail(string $id): \think\response\Json
    {
        if (empty($id) || $id == 'undefined') {
            throw new FailedException("缺少id参数");
        }
        return CatchResponse::success($this->patientInfo->getInOutData($id));
    }

    /**
     * 档案信息
     *
     * @time 2020年01月09日
     * @param string $id
     * @return \think\response\Json
     */
    public function archival_info(string $id): \think\response\Json
    {
        if (empty($id) || $id == 'undefined') {
            throw new FailedException("缺少id参数");
        }
        return CatchResponse::success($this->patientInfo->getWhereId($id));
    }

    /**
     * 院内医疗
     *
     * @time 2020年01月09日
     * @param string $id
     * @return \think\response\Json
     */
    public function courtyard_medical($id): \think\response\Json
    {
        if (empty($id) || $id == 'undefined') {
            throw new FailedException("缺少id参数");
        }
        $inhospital = (new PatientRecordModel)->where('id', $id)->field('type,inhospitalNo')->find();

        if ($inhospital) {
            if ($inhospital['type'] == 1) {
                $ret = (new PatientRecordModel)->where('id', $id)->find();
            } else {
                $ret = (new PatientRecordModel)->where([
                    ['inhospitalNo', '=', $inhospital['inhospitalNo']],
                    ['type', '=', 3]
                ])->findOrEmpty()->toArray();
                if (!$ret) {
                    $ret = (new PatientRecordModel)->where('id', $id)->find();
                }
            }
        } else {
            throw new FailedException('暂无数据');
        }
        return CatchResponse::success($ret);
    }

    /**
     * 获取关系
     * @return \think\response\Json
     */
    public function get_relation()
    {
        return CatchResponse::success((new RelationModel)->getDatas());
    }

    /**
     * 获取来源
     * @return \think\response\Json
     */
    public function get_source()
    {
        return CatchResponse::success((new SourceModel)->getDatas());
    }

    /**
     * 号码维护列表
     * @param string $id
     * @return \think\response\Json
     */
    public function maintenance_list(string $id): \think\response\Json
    {
        if (empty($id) || $id == 'undefined') {
            throw new FailedException("缺少id参数");
        }
        return CatchResponse::success($this->patientInfo->getNumberList($id));
    }

    /**
     * 添加号码维护
     * @return \think\response\Json
     */
    public function maintenance_save($id, CatchRequest $request)
    {
        $req = $request->param();

        if (empty($id) || $id == 'undefined') {
            throw new FailedException("缺少id参数");
        }

        if (!isset($req['mobile']) || empty($req['mobile'])) {
            throw new FailedException("电话号码不能为空");
        }

        //更新失访后的患者，联系方式更新状态
        (new InformationService())->upLostPhoneStatus($id, $req['mobile']);

        return CatchResponse::success($this->patientInfo->addNumber($id, $req));
    }

    /**
     * 更新号码维护
     * @return \think\response\Json
     */
    public function maintenance_up($id, CatchRequest $request)
    {
        $req = $request->param();

        if (empty($id) || $id == 'undefined') {
            throw new FailedException("缺少id参数");
        }

        if (!isset($req['mobile']) || empty($req['mobile'])) {
            throw new FailedException("电话号码不能为空");
        }
        $req['mobile'] = str_replace("-", "", $req["mobile"]);

        //更新失访后的患者，联系方式更新状态
        (new InformationService())->upLostPhoneStatus($id, $req['mobile']);

        return CatchResponse::success($this->patientInfo->upNumber($id, $req));
    }

    /**
     * 删除号码维护
     * @return \think\response\Json
     */
    public function maintenance_del($id, CatchRequest $request)
    {
        return CatchResponse::success($this->patientInfo->delNumber($id, $request->param()));
    }

    /**
     * 患者是否有随访任务
     *
     * @time 2023年02月28日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function maintenance_check(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();
        if (!array_key_exists('patientId', $req) || empty($req['patientId'])) {
            throw new FailedException("缺少patientId");
        }
        return CatchResponse::success((new DetailedService())->check($request->param('patientId')));
    }

    /**
     * 获取出院小结
     *
     * @time 2024年01月05日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function getDischargeSummary(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();
        if (!isset($req['inhospitalNo']) || empty($req['inhospitalNo'])) {
            throw new FailedException("住院号不能为空");
        }
        $ret = (new SummaryModel())->where('inhospitalNo', $req['inhospitalNo'])->find();

        if (!$ret) {
            $ret = [];
        }
        return CatchResponse::success($ret);
    }



    /**
     * 获取出院小结
     *
     * @time 2024年01月05日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function getOperation(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();
        if (!isset($req['inhospitalNo']) || empty(trim($req['inhospitalNo']))) {
            throw new FailedException("住院号不能为空");
        }
        $ret = (new Operation())->where('inhospitalNo', $req['inhospitalNo'])->order("operationDate", "desc")->select();

        return CatchResponse::success($ret);
    }

    /**
     * 生成ticket票据
     * @time 2024年01月09日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function generateTickets(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();

        if (!isset($req['patient_code']) && !isset($req['doctor_code'])) {
            throw new FailedException("缺少必要参数");
        }
        $ret['ticket'] = (new UserCenter())->generateTicket($req);
        return CatchResponse::success($ret);
    }
}
